package com.dayuan.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dayuan.bean.Address;
import com.dayuan.bean.UserInfo;
import com.dayuan.constant.AjaxMsg;
import com.dayuan.service.AddressService;
import com.dayuan.vo.ResultVo;

@Controller
@RequestMapping("/address")
public class AddressController {

	@Resource
	private AddressService addressService;

	/*
	 * private static Logger logger =
	 * LoggerFactory.getLogger(OrderController.class);
	 */

	/* 根据用户id查询address表 */
	@RequestMapping(value = "/selectaddressbyuserid.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo selectAddressByUserId(HttpSession session) {
		ResultVo resultVo = new ResultVo();
		try {
			UserInfo userInfo = (UserInfo) session.getAttribute("userinfo");
			List<Address> list = addressService.selectAddressByUserId(userInfo.getId());
			resultVo.setCode(AjaxMsg.SUCCESS.getValue());
			resultVo.setData(list);

			return resultVo;
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode(AjaxMsg.FAIL.getValue());
			resultVo.setMsg("网页消失在异次元");
			return resultVo;
		}
	}

	/* 增加寄货地址 */
	@RequestMapping(value = "/insertaddress.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo insertAddress(Address address) {
		ResultVo resultVo = new ResultVo();
		try {
			if (address.getReceiver() == null || address.getReceiver_phone() == null || address.getIs_default() == null
					|| address.getProvince() == null || address.getCity() == null || address.getArea() == null
					|| address.getDetailed_address() == null) {
				resultVo.setCode(AjaxMsg.FAIL.getValue());
				resultVo.setMsg("参数不能为空");
				return resultVo;
			}

			addressService.insertAddress(address);
			resultVo.setCode(AjaxMsg.SUCCESS.getValue());
			return resultVo;

		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode(AjaxMsg.FAIL.getValue());
			resultVo.setMsg("网页消失在异次元");
			return resultVo;
		}
	}

	/* 根据id删除收货地址 */
	@RequestMapping(value = "/deleteaddressbyid.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo deleteAddressById(Integer id) {
		ResultVo resultVo = new ResultVo();
		try {
			if (id == null) {
				resultVo.setCode(AjaxMsg.FAIL.getValue());
				resultVo.setMsg("参数不能为空");
				return resultVo;
			}
			addressService.deleteAddressById(id);
			resultVo.setCode(AjaxMsg.SUCCESS.getValue());
			return resultVo;
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode(AjaxMsg.FAIL.getValue());
			resultVo.setMsg("网页消失在异次元");
			return resultVo;
		}
	}

	/*修改地址*/ 
	@RequestMapping(value = "/updateaddressbyid.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo updateAddressById(Address address) {
		ResultVo resultVo = new ResultVo();
		try {
			if (address == null || address.getId() == null) {
				resultVo.setCode(AjaxMsg.FAIL.getValue());
				resultVo.setMsg("参数不能为空");
				return resultVo;
			}
			addressService.updateAddressById(address);
			resultVo.setCode(AjaxMsg.SUCCESS.getValue());
			return resultVo;
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode(AjaxMsg.FAIL.getValue());
			resultVo.setMsg("网页消失在异次元");
			return resultVo;
		}
	}

	/*修改默认地址*/ 
	@RequestMapping(value = "/updateaddressdefaultbyid.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo updateAddressDefaultById(Integer id, Integer oldId) {
		ResultVo resultVo = new ResultVo();
		try {
			if (id == null || oldId == null) {
				resultVo.setCode(AjaxMsg.FAIL.getValue());
				resultVo.setMsg("参数不能为空");
				return resultVo;
			}
			addressService.updateAddressDefaultById(id, oldId);
			resultVo.setCode(AjaxMsg.SUCCESS.getValue());
			return resultVo;
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode(AjaxMsg.FAIL.getValue());
			resultVo.setMsg("网页消失在异次元");
			return resultVo;
		}
	}
	
}
